package com.qf.boot2302.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

@RestController
@RequestMapping("/down")
@Slf4j
public class DownLoadController {

    @Value("${qf.fileupload.path}")
    private String filedir;

    @RequestMapping("/pic")
    public void download(String picname, HttpServletResponse response) throws Exception {

        // 获取文件名对应文件的路径
//        String filepath = filedir + "/" + picname;
        String filepath = filedir + File.separator + picname;
        log.info("path={}",filepath);
        // 添加响应头，让浏览器以附件的形式下载该文件
        response.addHeader("Content-Disposition","attachment;filename="+picname);
        // 将要下载的文件的字节数据拷贝到resonse对象的输出流中即可
        IOUtils.copy(new FileInputStream(filepath),response.getOutputStream());

    }

}
